package com.kaiwukj.android.ufamily.mvp.ui.page.sys.opinion;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerSystemComponent;
import com.kaiwukj.android.ufamily.di.module.SystemModule;
import com.kaiwukj.android.ufamily.mvp.ui.page.sys.SystemContract;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.SysOpinionUrl)
public class OpinionActivity extends BaseMvpActivity<OpinionPresenter> implements SystemContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_content_count)
    TextView tvContentCount;
    @BindView(R.id.et_phone)
    EditText etPhone;

    private final int maxTextCount = 200;


    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @OnClick(R.id.btn_submit)
    void submit() {
        String content = etContent.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();

        if (StringUtils.isEmpty(content)) {
            showMessage("反馈内容不能为空");
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            showMessage("联系方式不能为空");
            return;
        }
        mPresenter.postOpinion(content, phone);
        showLoading("正在提交...");
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerSystemComponent.builder()
                .appComponent(appComponent)
                .systemModule(new SystemModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_opinion;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("意见反馈");
        tvContentCount.setText(String.format("%s/%s", 0, maxTextCount));
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int tCount = etContent.getText().toString().length();
                tvContentCount.setText(String.format("%s/%s", tCount, maxTextCount));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void onTaskSuccess(int taskId) {
        super.onTaskSuccess(taskId);
        hideLoading();
        showMessage("提交成功");
        onBackPressedSupport();
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        hideLoading();
        showMessage("提交失败，请重试");
    }
}
